<template>
  <el-dialog :visible.sync="showDialog" width="40%" @close="onCancelAdd" append-to-body>
    <div style="line-height: 40px;font-size: 20px;font-weight: solid;margin: 20px 0;">手动添加</div>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="主机名称">
        <el-input v-model="form.HostName"></el-input>
      </el-form-item>
      <el-form-item label="操作系统">
        <el-input v-model="form.os"></el-input>
      </el-form-item>
      <el-form-item label="IP">
        <el-input v-model="form.ip"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio label="1">Running</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onCancelAdd">取 消</el-button>
      <el-button type="primary" @click="onAddServer">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>

import { AddServer } from '../../api/configure'

export default {
  name: 'tool-inner-add',
  props: {
    showInside: false
  },
  created() {
    this.showDialog = this.showInside
  },
  watch: {
    showInside: function(val) {
      this.showDialog = val
    }
  },
  data() {
    return {
      showDialog: false,
      form: {
        ip: '',
        status: '1',
        HostName: '',
        os: ''
      }
    }
  },
  methods: {
    onAddServer() {
      this.form.status = parseInt(this.form.status)
      AddServer(this.form).then(res => {
        if (res.code === 200) {
          this.$emit('serversChange', true)
        }
      }).catch(err => {
        console.log(err)
        this.$emit('serversChange', false)
        this.$notify({
          title: '失败',
          message: '获取失败',
          type: 'warning',
          duration: 2000
        })
      })
      this.form = {
        ip: '',
        status: '1',
        HostName: '',
        os: ''
      }
      this.showDialog = false
    },
    onCancelAdd() {
      this.form = {
        ip: '',
        status: '1',
        HostName: '',
        os: ''
      }
      this.showDialog = false
      this.$emit('serversChange', false)
    }
  }
}
</script>

